package com.qf.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.model.entity.Examprojecttable;
import com.qf.service.ExamprojecttableService;
import com.qf.util.R;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (Examprojecttable)表控制层
 *
 * @author makejava
 * @since 2024-05-01 09:31:28
 */
@RestController
@RequestMapping("/api/examprojecttable")
public class ExamprojecttableController {
    /**
     * 服务对象
     */
    @Autowired
    private ExamprojecttableService examprojecttableService;

    /**
     * 分页查询
     *
     * @return 查询结果
     */
    @RequestMapping("/list/page")
    public R queryByPage(@RequestParam Map<String,Object> map) {

        System.out.println("map = " + map);

        int pageNum = Integer.valueOf((String) map.get("pageNum"));
        int pageSize = Integer.valueOf((String) map.get("pageSize"));

        PageHelper.startPage(pageNum,pageSize);


        List<Examprojecttable> list = examprojecttableService.examprojecttableByCondition(map);

        if (list!=null&&list.size()>0){


            PageInfo<Examprojecttable> pageInfo = new PageInfo<>(list);

            return R.ok(pageInfo);

        }

        return R.fail("查询失败");
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    public R queryById(@PathVariable("id") Integer id) {
        return R.ok(this.examprojecttableService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param examprojecttable 实体
     * @return 新增结果
     */
    @RequestMapping("/save")
    public R add(Examprojecttable examprojecttable, HttpSession session) {

        boolean result = this.examprojecttableService.insert(examprojecttable,session);

        if (result){
            return R.ok();
        }

        return R.fail("添加失败");
    }

    /**
     * 编辑数据
     *
     * @param examprojecttable 实体
     * @return 编辑结果
     */
    @RequestMapping("/edit")
    public R edit(Examprojecttable examprojecttable) {

      boolean result =  this.examprojecttableService.update(examprojecttable);

      if (result){
          return R.ok();
      }

        return R.fail("更新失败");
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping("/del/{id}")
    public R deleteById(@PathVariable("id") Integer id) {

     boolean result =    this.examprojecttableService.deleteById(id);

     if (result){
         return R.ok();
     }

        return R.fail("删除失败");
    }


    @RequestMapping("del/batch")
    public R deleteBatch(@RequestParam  List<Integer> list){

     boolean result =    examprojecttableService.deleteBatch(list);

     if (result){
         return R.ok();
     }
     return R.fail("删除失败");

    }



    /**
     * 导出excel
     *
     * @param response
     */


    @RequestMapping("/examprojecttableExport")
    public void examprojecttableExport(HttpServletResponse response) {
        examprojecttableService.examprojecttableExport(response);
    }



    /**
     * 导入上传的 excel 文件
     *
     * @param file
     * @return
     * @RequestPart MultipartFile file 接受excel文件
     */

    @RequestMapping("/importExamprojecttable")
    public R importExamprojecttable(@RequestPart MultipartFile file) {

        return examprojecttableService.importExamprojecttable(file);


    }


}

